import { splitText } from '@/utils/splitText';
import { calcWordWidth } from '@/utils/calcWordWidth';

export const formatContent = (content: string | null, maxWidthInPixels: number, maxLines: number) => {
    if(content ===null){ 
        return {
            content: null,
            lines: null,
        }
    }
    const words = splitText(content);
    let lineLength = 0;
    let lines = 1;
    let formattedContent = "";
    if(words){
        for (let i = 0; i < words.length; i++) {
            const word = words[i];
            const wordWidth = calcWordWidth(word);
            // console.log(word,wordWidth);
            if (lineLength + wordWidth <= maxWidthInPixels) {
                formattedContent += word;
                lineLength += wordWidth; // 添加一个字符的宽度
            } else {
                // 换行
                lines++;
                if (lines > maxLines) {
                    // console.log(formattedContent);
                    let subNumber = wordWidth > 11 ? -1 : -2;
                    formattedContent = formattedContent.slice(0, subNumber) + '...'; // 使用省略号表示
                    break;
                }
                formattedContent = formattedContent.trim();
                formattedContent += "\n" + word;
                lineLength = wordWidth;
            }
        }
    }
    //   console.log(formattedContent);

    return {
        content: formattedContent.trim(),
        lines: lines,
    };
};